package com.zengqingfa.shardingjdbc.demo.service;

import cn.hutool.core.collection.CollectionUtil;
import com.zengqingfa.shardingjdbc.demo.entity.Bill;
import com.zengqingfa.shardingjdbc.demo.entity.BillDTO;
import com.zengqingfa.shardingjdbc.demo.entity.BillRespDTO;
import com.zengqingfa.shardingjdbc.demo.entity.DetailBill;
import com.zengqingfa.shardingjdbc.demo.mapper.BillMapper;
import com.zengqingfa.shardingjdbc.demo.mapper.DetailBillMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @fileName: BillServiceImpl
 * @author: zengqf3
 * @date: 2021-1-28 10:46
 * @description:
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;
    @Autowired
    private DetailBillMapper detailBillMapper;

    @Override
    public BillDTO create(BillDTO billDTO) {
        Bill bill = new Bill();
        BeanUtils.copyProperties(billDTO, bill);
        billMapper.insert(bill);
        List<DetailBill> detailBillList = billDTO.getDetailBillList();
        if (CollectionUtil.isNotEmpty(detailBillList)) {
            detailBillList.stream().forEach(d -> {
                d.setBillId(billDTO.getId());
                detailBillMapper.insert(d);
            });
        }
        return billDTO;
    }

    @Override
    public Bill selectByPrimary(Long id, String tenantCode) {
        return billMapper.selectByPrimary(id, tenantCode);
    }


    @Override
    public boolean deleteAll() {
        billMapper.delete(null);
        detailBillMapper.delete(null);
        return true;
    }

    @Override
    public List<BillRespDTO> queryBillAndDetail(Long id, String tenantCode) {
        return billMapper.queryBillAndDetail(id, tenantCode);
    }
}
